﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using Maticsoft.Common;
using System.Drawing;
using LTP.Accounts.Bus;
using System.IO;

namespace lcs.Web.inspectionMaterial
{
    public partial class List : Web.Admin.BasePage
    {
        
        
        
		lcs.BLL.inspectionMaterial bll = new lcs.BLL.inspectionMaterial();
        lcs.BLL.user bu = new BLL.user();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DataSet ds = bu.GetUserRoles(" roles_name='维修员'");
                ddldis.DataSource = ds.Tables[0];
                ddldis.DataTextField = "user_name";
                ddldis.DataValueField = "user_id";
                ddldis.DataBind();
                BindData();
                Page.DataBind();
            }
        }
        
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }
        protected void btndel_Click(object sender, EventArgs e)
        {
            string idlist = GetSelIDlist();
            if (idlist.Trim().Length == 0)
                return;
            bll.DeleteList(idlist);
            BindData();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            string idlist = GetSelIDlist();
            if (idlist.Trim().Length == 0)
                return;
            int uid = int.Parse(ddldis.SelectedValue.ToString());
            if (bll.Updateuid(uid, idlist))
            {
                BindData();
                Maticsoft.Common.MessageBox.Show(this, "分配成功!");
            }
        }
        protected void btnOUT_Click(object sender, EventArgs e)
        {
            int id = getid();
            DataSet ds = new DataSet();
            StringBuilder strWhere = new StringBuilder();
            if (txtKeyword.Text.Trim() != "")
            {
                strWhere.AppendFormat("and material like '%{0}%'", txtKeyword.Text.Trim());
            }
            if (txtdress.Text.Trim() != "")
            {
                strWhere.AppendFormat("and receive_address like '%{0}%'", txtdress.Text.Trim());
            }
            if (ddlfy.SelectedValue != "-1")
            {
                if (ddlfy.SelectedValue == "0")
                    strWhere.Append(" and dealUid = 0");
                else
                    strWhere.Append(" and dealUid <> 0");
            }
            string dep = getdep();
            strWhere.AppendFormat("and equipmentManage ='{0}'", dep);
            ds = bll.GetList(strWhere.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {                
                string file = ExWord.ExportGzp(ds);
                FileInfo DownloadFile = new FileInfo(file);
                Response.Clear();
                Response.ClearHeaders();
                Response.Buffer = false;
                Response.ContentType = "application/octet-stream";
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.Name, System.Text.Encoding.UTF8));
                Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
                Response.WriteFile(DownloadFile.FullName);
                Response.Flush();
                Response.End();
            }
            else
            {
                Maticsoft.Common.MessageBox.Show(this, "数据为空！");
            }

        }
        protected void btnDelete1_Click(object sender, EventArgs e)
        {
            string idlist = GetSelIDlist();
            if (idlist.Trim().Length == 0)
                return;
            string dtstr = txttime.Text.Trim();
            DateTime dt = DateTime.Now.AddDays(15);
            DateTime.TryParse(dtstr, out dt);
            string s = bll.UpdateTime(dt, idlist);
            BindData();
            if (s != "")
            {
                Maticsoft.Common.MessageBox.Show(this, s);
            }
            else
                Maticsoft.Common.MessageBox.Show(this, "设置成功!");

        }
        #region gridView

        public void BindData()
        {
            #region
            //if (!Context.User.Identity.IsAuthenticated)
            //{
            //    return;
            //}
            //AccountsPrincipal user = new AccountsPrincipal(Context.User.Identity.Name);
            //if (user.HasPermissionID(PermId_Modify))
            //{
            //    gridView.Columns[6].Visible = true;
            //}
            //if (user.HasPermissionID(PermId_Delete))
            //{
            //    gridView.Columns[7].Visible = true;
            //}
            #endregion

            DataSet ds = new DataSet();
            StringBuilder strWhere = new StringBuilder();
            if (txtKeyword.Text.Trim() != "")
            {
                strWhere.AppendFormat("and material like '%{0}%'", txtKeyword.Text.Trim());
            }
            if (txtdress.Text.Trim() != "")
            {
                strWhere.AppendFormat("and receive_address like '%{0}%'", txtdress.Text.Trim());
            }
            if (ddlfy.SelectedValue != "-1")
            {
                if (ddlfy.SelectedValue == "0")
                    strWhere.Append(" and (dealUid = 0 or dealUid is null)");
                else
                    strWhere.Append(" and dealUid <> 0");
            }
            string dep = getdep();
            strWhere.AppendFormat("and equipmentManage ='{0}' order by submitTime desc", dep);
            ds = bll.GetList(strWhere.ToString());            
            gridView.DataSource = ds;
            gridView.DataBind();
        }

        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindData();
        }
        protected void gridView_OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                //e.Row.Cells[0].Text = "<input id='Checkbox2' type='checkbox' onclick='CheckAll()'/><label></label>";
            }
        }
        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            e.Row.Attributes.Add("style", "background:#FFF");
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //LinkButton linkbtnDel = (LinkButton)e.Row.FindControl("LinkButton1");
                //linkbtnDel.Attributes.Add("onclick", "return confirm(\"你确认要删除吗\")");
                
                //object obj1 = DataBinder.Eval(e.Row.DataItem, "Levels");
                //if ((obj1 != null) && ((obj1.ToString() != "")))
                //{
                //    e.Row.Cells[1].Text = obj1.ToString();
                //}
               
            }
        }
        
        protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //#warning 代码生成警告：请检查确认真实主键的名称和类型是否正确
            //int ID = (int)gridView.DataKeys[e.RowIndex].Value;
            //bll.Delete(ID);
            //gridView.OnBind();
        }

        private string GetSelIDlist()
        {
            string idlist = "";
            bool BxsChkd = false;
            for (int i = 0; i < gridView.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)gridView.Rows[i].FindControl("DeleteThis");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {
                    BxsChkd = true;
                    //#warning 代码生成警告：请检查确认Cells的列索引是否正确
                    if (gridView.DataKeys[i].Value != null)
                    {                        
                        idlist += gridView.DataKeys[i].Value.ToString() + ",";
                    }
                }
            }
            if (BxsChkd)
            {
                idlist = idlist.Substring(0, idlist.LastIndexOf(","));
            }
            return idlist;
        }

        #endregion





    }
}
